#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname extra $PACKAGE); if (($? != 0)); then exit 1; fi;
PKG_VERSION=$(pkgversion extra $PACKAGE); if (($? != 0)); then exit 2; fi;
PKG_ARCHIVE=$(pkgarchive extra $PACKAGE); if (($? != 0)); then exit 3; fi;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE); if (($? != 0)); then exit 4; fi;
PKG_DEST=/usr/pkg/$PKG_NAME/$PKG_VERSION;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
tar -xf ../tcl${PKG_VERSION}-html.tar.gz --strip-components=1



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
export SRCDIR=`pwd` && \
cd unix && \
./configure --prefix=/usr           \
            --without-tzdata        \
            --mandir=/usr/share/man \
            $([ $(uname -m) = x86_64 ] && echo --enable-64bit) \
	&& \
	make && \
    rm -vRf $PKG_DEST/inst && \
    mkdir -pv $PKG_DEST/inst/usr/{lib,bin,include} && \
	sed -e "s#$SRCDIR/unix#/usr/lib#" \
		-e "s#$SRCDIR#/usr/include#"  \
		-i tclConfig.sh               && \
	sed -e "s#$SRCDIR/unix/pkgs/tdbc1.0.1#/usr/lib/tdbc1.0.0#" \
		-e "s#$SRCDIR/pkgs/tdbc1.0.1/generic#/usr/include#"    \
		-e "s#$SRCDIR/pkgs/tdbc1.0.1/library#/usr/lib/tcl8.6#" \
		-e "s#$SRCDIR/pkgs/tdbc1.0.1#/usr/include#"            \
		-i pkgs/tdbc1.0.1/tdbcConfig.sh                        && \
	sed -e "s#$SRCDIR/unix/pkgs/itcl4.0.1#/usr/lib/itcl4.0.0#" \
		-e "s#$SRCDIR/pkgs/itcl4.0.1/generic#/usr/include#"    \
		-e "s#$SRCDIR/pkgs/itcl4.0.1#/usr/include#"            \
		-i pkgs/itcl4.0.1/itclConfig.sh                        && \
	unset SRCDIR && \
	make DESTDIR=$PKG_DEST/inst install && \
	make DESTDIR=$PKG_DEST/inst install-private-headers && \
	ln -v -sf tclsh$(mmver $PKG_VERSION) $PKG_DEST/inst/usr/bin/tclsh && \
	chmod -v 755 $PKG_DEST/inst/usr/lib/libtcl$(mmver $PKG_VERSION).so && \
	mkdir -v -p $PKG_DEST/inst/usr/share/doc/tcl-${PKG_VERSION} && \
	cp -v -r  ../html/* $PKG_DEST/inst/usr/share/doc/tcl-${PKG_VERSION}



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
# BLK3



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
echo "# Package information file... generated at make process" > $PKG_DEST/pkg.info && \
echo "# pkg name   source archive    version" >> $PKG_DEST/pkg.info && \
echo "$PKG_NAME  $PKG_ARCHIVE  $PKG_VERSION" >> $PKG_DEST/pkg.info && \
if [ -d $LFS/sources/pkg/extra/$PKG_NAME ]; then cp -aRv $LFS/sources/pkg/extra/$PKG_NAME/* $PKG_DEST; fi && \
cp -avf $LFS/sources/pkg/pkgutils.sh $PKG_DEST/pkgutils.sh && \
tar -czvvf /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz -C /usr/pkg/$PKG_NAME $PKG_VERSION && \
rm -Rfv /usr/pkg/$PKG_NAME && \
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
echo "Build complete extra/$PACKAGE"
echo "Package files are stored to: /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz"
echo " --OK--"
